/************************公告内容制作js*********************************/
define(function(require, exports, module) {
	
	var $ = require("jquery1.7");
	var utils = require("utils");
	var ctx = utils.constants.ctx;
	var Validator = require("validator");
	var ueditor = require("ueditor");
	var internalEmailService = require("bean/internalEmailService");
	var nickName = $("#nickName").val();
	var emailId=$("#emailId").val();
	var addressee = $("#addressee").val();
	var BlackBox = require("blackbox");
	var blackBox = new BlackBox();
	
	var linkManList = [];
	if(emailId!=null||emailId!=""){
		$(function(){
			linkManList=addressee.split(",");
			linkManList.splice(linkManList.length-1);
		});
	}
	
	var con_validator = new Validator({
        element: $("#noticeContentForm")
    }).addItem({
        element: '[name=emailTitle]',
        required: true
    });
	
	$(".addLinkMan").bind("click",function(){
		var linkManId=$(".am-checked").attr("data-value");
		var linkMan=$(".am-selected-status").html();
		if(linkManId==0){
			blackBox.alert("请选择收件人");
			return;
		}
		var inx=$(".p_addressee").length;
		for(var i=0;i<inx;i++){
			var id = $(".p_addressee").eq(i).find("span").attr("data");
			if(linkManId==id){
				blackBox.alert("该收件人已存在！！");
				return;
			}
		}
		var htm = $(".p_addresseeList").html();
		htm += '<div class="p_addressee"><span data="'+linkManId+'">'+linkMan+'</span><a class="p_addresDelete"></a></div>'+
		          '<div class="clear"></div>';
		
		$(".p_addresseeList").html(htm);
		linkManList.push(linkManId);
	});
	
	$(document).on("click", ".p_addresDelete", function(e) {
		var inx = $(".p_addressee").index($(this).closest(".p_addressee"));
		$(this).parent().next().remove();
		$(this).parent().remove();
		linkManList.splice(inx,1);
	});
	
	$("#sureAddBtn").bind("click",function(){
		var conten;
		ue.getKfContent(ue, function(content) {
			conten = content;
		});
		var emailTitle=$("#emailTitle").val();
		if(emailTitle==""){
			blackBox.alert("请输入标题");
			return;
		}
		if(linkManList.length==0){
			blackBox.alert("请选择收件人");
			return;
		}
		var addressee = ""
			for(var i=0;i<linkManList.length;i++){
				addressee += linkManList[i]+",";
			}
		
		
		
		if(conten==""){
			blackBox.alert("请输入公告内容");
			return;
		}
		
		// email实体
		var email = {
				title : emailTitle,
				publisher : nickName,
				addressee : addressee,
				content : conten
		}
		
		try{
			if(emailId==null||""==emailId){
				internalEmailService.addEmail(email);
				blackBox.alert("添加成功");
				location.href = ctx + "/back/website/internalEmailManager";
			}else{
				email["id"]=emailId;
				internalEmailService.updateEmail(email);
				blackBox.alert("修改成功");
				location.href = ctx + "/back/website/internalEmailManager";
			}
		}catch(e){
			blackBox.alert(e.message);
		}
	});
	
	//取消按钮
	$("#cancelAddBtn").bind("click",function(){
		location.href = ctx + "/back/website/internalEmailManager";
	});
});